package one.two.ten;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class VisualCounter implements Comparable<VisualCounter> {

	private final String name; // counter name
	private int count = 0; // current value

	private int n;
	private int o;
	private int om;

	public VisualCounter(String id, int n, int om) {
		name = id;
		this.n = n;
		o = 0;
		this.om = om;
	}

	public void increment() {
		if (o < om && count < n) {
			count++;
			o++;

			double e = ((double) count) / ((double) (2 * n));

			StdDraw.filledRectangle(0.5, e, 0.1, e);
			StdDraw.pause(1000 * 10);
			StdDraw.clear();

		}
	}

	public int tally() {
		return count;
	}

	public String toString() {
		return count + " " + name;
	}

	@Override
	public int compareTo(VisualCounter that) {
		if (this.count < that.count)
			return -1;
		else if (this.count > that.count)
			return +1;
		else
			return 0;
	}

	public static void main(String[] args) {
		VisualCounter c = new VisualCounter("n", 10, 100);
		c.increment();

		/*
		 * int n = Integer.parseInt(args[0]); int trials = Integer.parseInt(args[1]);
		 * 
		 * // create n counters Counter[] hits = new Counter[n]; for (int i = 0; i < n;
		 * i++) { hits[i] = new Counter("counter" + i); }
		 * 
		 * // increment trials counters at random for (int t = 0; t < trials; t++) {
		 * hits[StdRandom.uniform(n)].increment(); }
		 * 
		 * // print results for (int i = 0; i < n; i++) { StdOut.println(hits[i]); }
		 */
	}
}